//
//  AlertDataView.m
//  StationManager
//
//  Created by 泽华伟业 on 17/3/9.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "AlertDataView.h"
#import "DataHandel.h"
#import "AlertTabeleViewCell.h"
#import "WindTableViewCell.h"

@interface AlertDataView ()<UITableViewDelegate,UITableViewDataSource>

//数据保护要素数量（最多气温、降水、风三个，最少一个）
@property (nonatomic,assign) int elementNumber;
//站号，由bottom视图传过来
@property (nonatomic,copy) NSString *stationNum;

@property (nonatomic,strong) UISegmentedControl *segmentedControl;
//展示列表
@property (nonatomic,strong) UITableView *dataTableView;
//数据数组
@property (nonatomic,strong) NSArray *staDataArray;


@end

@implementation AlertDataView

- (instancetype)initWithElementNumber:(int)elementNumber StationNum:(NSString *)stationNum
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 10;
        _elementNumber = elementNumber;
        _stationNum = stationNum;
        [self segmentedControl];
//        _staDataArray = [NSArray array];
        //请求数据
        [self getStationDataWithElementName:@"降水"];
    }
    return self;
}

- (UISegmentedControl *)segmentedControl{
    if (!_segmentedControl) {
        //根据Bottom传过来的数据确定控件按钮的个数
        switch (_elementNumber) {
            case 1:
                _segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"降水"]];
                break;

            case 2:
                _segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"降水",@"气温"]];
                break;

            case 3:
                _segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"降水",@"气温",@"风"]];
                break;

            default:
                break;
        }
        [self addSubview:_segmentedControl];
        _segmentedControl.selectedSegmentIndex = 0;
        _segmentedControl.tintColor = RGBCOLOR(98, 149, 222);
        [_segmentedControl addTarget:self action:@selector(segmentedControlChengSelected:) forControlEvents:UIControlEventValueChanged];
        [_segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).offset(15*hl);
            make.size.mas_equalTo(CGSizeMake(50*wl *_elementNumber, 30*hl));
        }];
    }
    return _segmentedControl;
}

- (UITableView *)dataTableView{
    if (!_dataTableView) {
        _dataTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 200, 200) style:UITableViewStylePlain];
        [self addSubview:_dataTableView];
        _dataTableView.delegate = self;
        _dataTableView.dataSource = self;
        _dataTableView.scrollEnabled = NO;
        if ([self.dataTableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.dataTableView setSeparatorInset:UIEdgeInsetsZero];
        }
        if ([self.dataTableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [self.dataTableView setLayoutMargins:UIEdgeInsetsZero];
        }
        [_dataTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.top.equalTo(_segmentedControl.mas_bottom).offset(5*hl);
        }];
    }
    return _dataTableView;
}

#pragma makr -- segmentControl点击
- (void)segmentedControlChengSelected:(UISegmentedControl *)segmentControl{
    if (segmentControl.selectedSegmentIndex == 0) {
        HLog(@"0");
        [self getStationDataWithElementName:@"降水"];
    }else if(segmentControl.selectedSegmentIndex == 1){
        HLog(@"1");
        [self getStationDataWithElementName:@"气温"];
    }else if (segmentControl.selectedSegmentIndex == 2){
        HLog(@"2");
        [self getStationDataWithElementName:@"风"];
    }
}

#pragma mark -- tableView代理方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    AlertTabeleViewCell *cell = [[AlertTabeleViewCell alloc]init];
    cell.contentView.backgroundColor = RGBCOLOR(98, 149, 222);
    if (_segmentedControl.selectedSegmentIndex == 0) {
        [cell setLabelTextWithData:@"降水" Time:@"观测时间"];
    }else if(_segmentedControl.selectedSegmentIndex == 1){
        [cell setLabelTextWithData:@"气温" Time:@"观测时间"];
    }else if (_segmentedControl.selectedSegmentIndex == 2){
        WindTableViewCell *windCell = [[WindTableViewCell alloc]init];
        windCell.contentView.backgroundColor = RGBCOLOR(98, 149, 222);
        [windCell setTextForLabelWithDirection:@"风向" Speed:@"风速" Time:@"观测时间"];
        return windCell.contentView;
    }
    return cell.contentView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25*hl;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 26.5*hl;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _staDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_segmentedControl.selectedSegmentIndex == 2) {
        WindTableViewCell *windCell = [tableView dequeueReusableCellWithIdentifier:@"windTableView"];
        if (!windCell) {
            windCell = [[WindTableViewCell alloc]init];
        }
        NSDictionary *dict = _staDataArray[indexPath.row];
        [windCell setTextForLabelWithDirection:[NSString stringWithFormat:@"%@",[dict objectForKey:@"风向"]] Speed:[NSString stringWithFormat:@"%@",[dict objectForKey:@"风速"]] Time:[dict objectForKey:@"观测时间"]];
        return windCell;
    }else{
        AlertTabeleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"alertViewTableViewCell"];
        if (!cell) {
            cell = [[AlertTabeleViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"alertViewTableViewCell"];
        }
        if (_segmentedControl.selectedSegmentIndex == 0) {
            NSDictionary *dict =_staDataArray[indexPath.row];
            [cell setLabelTextWithData:[NSString stringWithFormat:@"%@",[self getShortStringWithLongString:[dict objectForKey:@"降水"]]] Time:[dict objectForKey:@"观测时间"]];
        }else if (_segmentedControl.selectedSegmentIndex == 1){
            NSDictionary *dict = _staDataArray[indexPath.row];
            [cell setLabelTextWithData:[NSString stringWithFormat:@"%@",[self getShortStringWithLongString:[dict objectForKey:@"气温"]]] Time:[dict objectForKey:@"观测时间"]];
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
////cell将要显示时调用
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

//获取数据方法
- (void)getStationDataWithElementName:(NSString *)elementName{
    [[DataHandel sharedDataHandel]getStationDataByStationNum:_stationNum ElementType:elementName showNumber:10 StationBlock:^(id object) {
        NSMutableArray *muArray = [NSMutableArray array];
        for (NSDictionary *dict in object) {
            NSMutableDictionary *mutableDict = [NSMutableDictionary dictionaryWithDictionary:dict];
            NSString *timeStr =[mutableDict objectForKey:@"观测时间"];
            NSArray *strArray = [timeStr componentsSeparatedByString:@"T"];
            [mutableDict setObject:[NSString stringWithFormat:@"%@  %@",strArray.firstObject,strArray.lastObject] forKey:@"观测时间"];
            [muArray addObject:mutableDict];
        }
        _staDataArray = [NSArray arrayWithArray:muArray];
        [self.dataTableView reloadData];
    }];
}

//处理数据为多位小数的情况
- (NSString *)getShortStringWithLongString:(NSString *)string{
    float num = [string floatValue];
    NSString *shortStr = [NSString stringWithFormat:@"%.1f",num];
    return shortStr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
